<template>
  <div style="display: flex; justify-content: center">
    <div style="height: 300px; width: 100%;">
      <el-row style="display: flex; justify-content: center; margin-bottom: 20px">
        <span style="font-size: 20px;">个人信息</span>
      </el-row>
      <!--头像-->
      <el-row :gutter="20">
        <el-col :span="3">
          头像
        </el-col>
        <el-col :span="4">
          <el-avatar :src="head"/>
        </el-col>
        <el-col :span="13">
          <el-row :gutter="13">
            <el-upload :before-upload="getFile" :show-file-list="false" action="">
              <el-button size="mini" type="primary" plain>上传新头像</el-button>
              <br>
              <span style="color: #868686; font-size: 12px">支持JPG、PNG、GIF格式，小于2MB</span>
            </el-upload>
          </el-row>
        </el-col>
      </el-row>
      <!--账号-->
      <el-row :gutter="20">
        <el-col :span="3">
          账号
        </el-col>
        <el-col :span="17">{{this.username}}</el-col>
      </el-row>
      <!--昵称-->
      <el-row :gutter="20">
        <el-col :span="3">
          昵称
        </el-col>
        <el-col :span="17">
          <el-input v-model="nickname" id="nickName"/>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="3">
          性别
        </el-col>
        <el-col :span="17">
          <el-radio v-model="sex" label="女">女</el-radio>
          <el-radio v-model="sex" label="男">男</el-radio>
          <el-radio v-model="sex" label="保密">保密</el-radio>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="3">
          地区
        </el-col>
        <el-col :span="8">
          <el-select placeholder="省" v-model="province" id="province" value=""
                     @change="getCityOfTheProvince(province)">
            <el-option
              v-for="item in this.globalVariable.cityInfo"
              :key="item.label"
              :label="item.label"
              :value="item.label">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="8">
          <el-select placeholder="市" v-model="city" id="city" value="">
            <el-option
              v-for="item in this.cityOfProvince"
              :key="item.label"
              :label="item.label"
              :value="item.label">
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row :gutter="20" style="height: 80px">
        <el-col :span="3">签名</el-col>
        <el-col :span="17" style="display: inline-block;margin-top: 20px">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="introduction">
          </el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11" style="display: flex; justify-content: center">
          <el-button type="primary" @click="editPersonalInformation">确认</el-button>
        </el-col>
        <el-col :span="11" style="display: flex; justify-content: center">
          <el-button plain style="border: 1px solid #DCDFE6">取消</el-button>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
  export default {
    props: {
      page: ''
    },
    data() {
      return {
        username: "",
        nickname: "",
        province: "",
        city: "",
        cityOfProvince: [],
        introduction: "",
        sex: "保密",
        head: '',
        headPicture: File
      }
    },
    name: "PersonalInformation",
    methods: {
      getPersonalInformation() {
        this.apis.getPersonalInformation(res => {
          this.username = res.username === null ? "" : res.username;
          this.head = res.picture === null ? "" : res.picture;
          this.nickname = res.nickname === null ? "" : res.nickname;
          this.province = res.province === null ? "" : res.province;
          this.city = res.city === null ? "" : res.city;
          this.introduction = res.introduction === null ? "" : res.introduction;
          this.sex = "" + (res.sex === null ? "保密" : (res.sex === true ? "男" : "女"));
          this.page.personalInformation.head = this.head;
          this.page.updateFileTree();
          this.page.getAllSigns();
          console.log(this.sex)
        });
      },

      editPersonalInformation() {
        console.log(this.headPicture);
        this.apis.modifyPersonalInformation(this.headPicture, this.nickname, this.sex, this.province, this.city, this.introduction,
          res => {
            this.showMessage(res);
            this.getPersonalInformation();
          }
        );
      },

      getCityOfTheProvince(val) {
        console.log(this.globalVariable);
        for (let i = 0; i < this.globalVariable.cityInfo.length; i++)
          if (this.globalVariable.cityInfo[i].label === val) {
            this.cityOfProvince = this.globalVariable.cityInfo[i]['children'];
            return;
          }
      },
      getFile(file) {
        // console.log(file);
        this.head = URL.createObjectURL(file);
        this.headPicture = file;
        console.log(this.head);
      }
    },
    created() {
      this.getPersonalInformation()
    },
    updated() {
    }
  }
</script>

<style>

  .el-input.el-input--suffix {
    width: 100%;
  }

  .el-col {
    align-content: center;
    align-items: center;
    display: flex;
    height: 40px;
    border-radius: 4px;
  }

  .bg-purple-dark {
    background: #99a9bf;
  }

  .bg-purple {
    background: #d3dce6;
  }

  .bg-purple-light {
    background: #e5e9f2;
  }

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>
